/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.model;

import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.jcr.AccessDeniedException;
import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import offset.nodes.client.virtual.model.jcr.QName;

/**
 *
 * @author Walter Lütgenau
 */
public class SimpleNamespaceRegistry implements NamespaceRegistry {

    static SimpleNamespaceRegistry instance = null;
    HashMap<String, String> toURI = null;
    HashMap<String, String> toPrefix = null;

    public static SimpleNamespaceRegistry getInstance() {
        if (instance == null)
            synchronized (SimpleNamespaceRegistry.class) {
                Properties namespaces = new Properties();
                InputStream input = SimpleNamespaceRegistry.class.getResourceAsStream("namespaces.properties");
                try {
                    namespaces.load(input);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Enumeration e = namespaces.propertyNames();

                HashMap<String, String> toURI = new HashMap<String,String>();
                HashMap<String, String> toPrefix = new HashMap<String,String>();

                while (e.hasMoreElements()) {
                    String key = (String) e.nextElement();
                    toURI.put(key, namespaces.getProperty(key));
                    toPrefix.put(namespaces.getProperty(key), key);
                }
                toURI.put("", "");
                toPrefix.put("", "");

                instance = new SimpleNamespaceRegistry(toURI, toPrefix);
            }

        return instance;
    }

    public SimpleNamespaceRegistry(HashMap<String, String> toURI, HashMap<String, String> toPrefix) {
        this.toURI = toURI;
        this.toPrefix = toPrefix;
    }

    public void unregisterNamespace(String prefix) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
    }

    public String getURI(String prefix) {
        return (String) toURI.get(prefix);
    }

    public String getPrefix(String uri) {
        return (String) toPrefix.get(uri);
    }

    public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
        toURI.put(prefix, uri);
        toPrefix.put(uri, prefix);
    }

    public String[] getURIs() throws RepositoryException {
        Collection values = toURI.values();
        String[] result = new String[values.size()];
        Iterator it = values.iterator();

        int i = 0;
        while (it.hasNext()) {
            result[i++] = (String) it.next();
        }

        return result;
    }

    public String[] getPrefixes() throws RepositoryException {
        Collection values = toPrefix.values();
        String[] result = new String[values.size()];
        Iterator it = values.iterator();

        int i = 0;
        while (it.hasNext()) {
            result[i++] = (String) it.next();
        }

        return result;
    }

    public String toPrefixName(QName qname) {
        try {
            if (qname.equals(QName.ANY_NAME) || qname.getNamespaceURI() == null || qname.getNamespaceURI().length() == 0)
                return qname.getLocalName();
            return getPrefix(qname.getNamespaceURI()) + ":" + qname.getLocalName();
        } catch (Exception e) {
            e.printStackTrace();
            return qname.toString();
        }
    }

    public String toQNameString(String prefixName) {
        QName name = toQName(prefixName);
        if (name == null)
            return prefixName;

        return name.toString();
    }

   public QName toQName(String prefixName) {
        if (prefixName.indexOf(":") < 0)
            return null;

        String[] parts = prefixName.split(":");
        QName name = null;
        try {
            name = new QName(getURI(parts[0]), parts[1]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return name;
    }
}
